<?php require_once 'links.inc' ?>
<?php require_once 'html_template.php' ?>
<?php

define("MIN", 0);
define("MAX", 10);
define("USER_ANSWER", "user_answer");
define("CORRECT_RESULT", "correct_result");
define("ROUND_NUMBER", "round_number");
define("CORRECT_ANSWERS_COUNTER", "correct_answers_counter");
define("SUBMIT", "ok");


function get_mathematical_task($a, $b, $operator)
{
	$text = array();
	$text[] = "Bitte berechnen Sie:<br />\n<br />\n $a $operator $b = ";
	if (strcmp($operator, "+") == 0)
	{
		$correct_result = $a + $b;
	} 
	else if (strcmp($operator, "*") == 0)
	{
		$correct_result = $a * $b;
	} 
	else
	{ 
		print_r("Fehler, der Operator $operator ist nicht definiert.");
	}
	$text[] = "<input type='hidden' name='".CORRECT_RESULT."' value='".$correct_result."' /><br />\n";
	return $text;	
}

function get_form_content($number_1, $number_2, $operator)
{
	$form_content = array();
	$form_content[] = get_mathematical_task($number_1, $number_2, $operator);
	$form_content[] = "<input type='text' name='".USER_ANSWER."' /><br />\n";
	$form_content[] = "<input type='submit' name='".SUBMIT."' value='OK' /><br />\n";
	return implode(" ", $form_content);
}

function check_user_input($user_input, $correct_result){
	echo 'checke deine antwort<br /><br />\n';
	return $user_input == $correct_result; 	
}

// ---------------------------------------------------------------------
// Variablen auslesen und belegen
// ---------------------------------------------------------------------
// formularfelder und text generieren

// rundennummer ermitteln, basis fuer checkk auf 1. klick
$round_number = isset($_REQUEST[ROUND_NUMBER]) ? intval($_REQUEST[ROUND_NUMBER]) : 1;


// html template ersetzungen
$title = "Kopfrechner";
$body = "";

// form template ersetzungen
$action = $_SERVER['PHP_SELF'];
$method = "get";
$form_content = "";

$number_1 = rand(MIN, MAX);
$number_2 = rand(MIN, MAX);
$operator = $round_number < 4 ? '+' : '*';

// Request-Daten (GET oder POST) auslesen / Formularfelder + hidden fields 
$user_answer = $round_number==1 ? NULL : $_REQUEST[USER_ANSWER];
$correct_answers_counter = $round_number==1 ? 0 : $_REQUEST[CORRECT_ANSWERS_COUNTER];
$correct_result = $round_number==1 ? NULL : $_REQUEST[CORRECT_RESULT];


// ---------------------------------------------------------------------
// Auswertung und Berechnungen
// ---------------------------------------------------------------------

// userergebnis pruefen und counter setzen 
if ($round_number != 1){	
	$correct_answers_counter = check_user_input($user_answer, $correct_result) ? $correct_answers_counter + 1 : $correct_answers_counter;
	
}
echo "somit hast du $correct_answers_counter richtig!";


// ergebnis der aktuellen runde berrechnen und setzen
$form_content .= "<input type='hidden' name='".CORRECT_RESULT."' value='".$correct_result."'/><br />\n";

// runde immer incrementieren in hiddenfild merken (noch kenie sessions erlaubt in der aufgabe)
$form_content .= "<input type='hidden' name='".ROUND_NUMBER."' value='".strval($round_number+1)."'/><br />\n";
$form_content .= "<input type='hidden' name='".CORRECT_ANSWERS_COUNTER."' value='".$correct_answers_counter."'/><br />\n";

// folgeseite ermitteln (Aufgabe oder Ergebnisuebersicht)

// ---------------------------------------------------------------------
// Ausgabe zusammenbauen und ausgeben
// ---------------------------------------------------------------------

// formularfelder und text generieren
$form_content .= get_form_content($number_1, $number_2, $operator);

// formularfelder und text in template ersetzen
$form = str_replace(array("{action}", "{method}", "{form_content}"), array($action, $method, $form_content), $form_template);
$body .= $form;

echo str_replace(array("{title}", "{body}"), array($title, $body), $html_template);